package com.lq.schoolshop.controller.before;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.controller.BaseController;
import com.lq.schoolshop.pojo.BigScreen;
import com.lq.schoolshop.pojo.Expand.OrderAndOrderDetailAndStore;
import com.lq.schoolshop.pojo.Expand.SendGoodStatus;
import com.lq.schoolshop.pojo.Expand.UserExamp;
import com.lq.schoolshop.pojo.Order;
import com.lq.schoolshop.service.PersonalCenterService;
import com.lq.schoolshop.service.UserService;
import com.lq.schoolshop.util.JwtUtil;
import com.lq.schoolshop.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @Author
 * @Date2021/1/13 11:39
 * @Version V1.0
 * 个人中心页面
 **/
@Api(value = "商城前台功能--个人中心模块",tags = "商城前台功能--个人中心模块")
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("personalcenter")
@Controller
public class ForPersonalCenterController extends BaseController {

    @Autowired
    private PersonalCenterService personalCenterService;

    @Autowired
    private UserService userService;

    @Autowired
    private MinioUtil minioUtil;

    //1.加载发货信量息-待发货 已发货 已完成
    @ApiOperation(value = "加载发货数量信息",notes = "用来加载个人中心发货量-待发货 已发货 已完成")
    @ResponseBody
    @RequestMapping(value = "/ShowSentGoodCount",method = RequestMethod.GET)
    public Result ShowSentGoodCount(@RequestHeader("define-token") String language){
        Result result = null;
        int userIDByToken = getUserIDByToken(language);
        SendGoodStatus sendGoodStatus = personalCenterService.selByWaitGoodsAndSendGoodAndFinish(userIDByToken);
        result = new Result(true,StatusCode.OK,"成功加载发货信息",sendGoodStatus);
        return result;
    }


    //2.加载我的全部订单信息
    @ApiOperation(value = "加载我的订单信息",notes = "加载我购买的的全部订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页面",dataType = "Integer",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "显示数量",dataType = "Integer",required = true,paramType = "query"),
            @ApiImplicitParam(name = "flag",value = "查询标记",dataType = "Integer",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/ShowAllOrderInfo",method = RequestMethod.POST)
    public Result ShowAllOrderInfo(@RequestParam("pageNum") int pageNumint,@RequestParam("pageSize") int pageSizeint,
                                   @RequestParam("flag") int flag,@RequestHeader("define-token") String language){
        Result result = null;
        int userint = getUserIDByToken(language);
        if(flag==1){  //查询全部订单
            PageHelper.startPage(pageNumint, pageSizeint);
            List<OrderAndOrderDetailAndStore> orderAndOrderDetailAndStores = personalCenterService.selByAll(userint);
            PageInfo pageInfo = new PageInfo(orderAndOrderDetailAndStores);
            result = new Result(true,StatusCode.OK,"成功加载订单信息",pageInfo);
        }else if(flag==2){    //根据待发货查询
            PageHelper.startPage(pageNumint, pageSizeint);
            List<OrderAndOrderDetailAndStore> orderAndOrderDetailAndStores2 = personalCenterService.selNotSentByAll(userint);
            PageInfo pageInfo = new PageInfo(orderAndOrderDetailAndStores2);
            result = new Result(true,StatusCode.OK,"成功加载订单信息",pageInfo);
        }else if(flag==3){  //根据已发货查询
            PageHelper.startPage(pageNumint, pageSizeint);
            List<OrderAndOrderDetailAndStore> orderAndOrderDetailAndStores3 = personalCenterService.selSentByAll(userint);
            PageInfo pageInfo = new PageInfo(orderAndOrderDetailAndStores3);
            result = new Result(true,StatusCode.OK,"成功加载订单信息",pageInfo);
        }else if(flag==4){ //根据已完成查询
            PageHelper.startPage(pageNumint, pageSizeint);
            List<OrderAndOrderDetailAndStore> orderAndOrderDetailAndStores4 = personalCenterService.selSuccessSentByAll(userint);
            PageInfo pageInfo = new PageInfo(orderAndOrderDetailAndStores4);
            result = new Result(true,StatusCode.OK,"成功加载订单信息",pageInfo);
        }else {  //全部都不是 说明错误
            result = new Result(true,StatusCode.OK,"失败,加载订单信息错误");
        }

        return  result;   
    }

    //查询出该用户的所有信息
    @ApiOperation(value = "查询用户全部信息",notes = "查询用户全部信息显示在个人中心")
    @ResponseBody
    @RequestMapping(value = "/ShowAllUserInfo",method = RequestMethod.POST)
    public Result ShowAllUserInfo(@RequestHeader("define-token") String language){
        int userint = getUserIDByToken(language);
        UserExamp showall = userService.showall(userint);
        return new Result(true,StatusCode.OK,"成功",showall);
    }


    //3.1 用户修改-修改用户头像
    @ApiOperation(value = "修改用户头像",notes = "用来修改个人中心的用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件",dataType = "MultipartFile",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/updateUserIcon",method = RequestMethod.POST)
    public Result updateUserIcon(@RequestParam("file") MultipartFile file,@RequestHeader("define-token") String language){
        Result result = null;
        if(file.isEmpty()){
            result = new Result(false,StatusCode.ERROR,"上传失败，请选择文件");
        }else {
            int userint = getUserIDByToken(language);
            //1.将文件名字换一下。
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
            //2.生成随机名
            String fileName = UUID.randomUUID() + extension;
            String finallyfile ="http://39.105.74.232:9000/user"+"/"+fileName;

            System.out.println("文件名:"+finallyfile);
            //3.将文件传到minio服务器上
            //3.2 查询出原来的头像 删除在服务器上面的头像。
            //boolean user = minioUtil.removeObject("user", "1111"); //删除原来的头像
            minioUtil.putObject("user",file,fileName);
            //4.将数据库的头像地址修改  (还未完成)
            int i = userService.updateImg(userint, finallyfile);
            //5.查询出更改之后的头像返回给前台显示
            String resultimg = userService.getlogoImgById(userint);
            result = new Result(true,StatusCode.OK,"成功",resultimg);
        }
        return result;
    }

    //3.2 用户修改-修改用户姓名、手机号、物流地址
    @ApiOperation(value = "修改用户信息",notes = "修改用户姓名、手机号、物流地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户姓名",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "用户手机号",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "address",value = "邮寄地址",dataType = "String",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/updateUserInfo",method = RequestMethod.POST)
    public Result updateUserInfo(@RequestParam("name") String name,@RequestParam("phone") String phone,@RequestParam("address") String address,@RequestHeader("define-token") String language){
        Result result = null;
        int userint = getUserIDByToken(language);
        int updateuserinfo = userService.updateuserinfo(name, phone, address, userint);
        return new Result(true,StatusCode.OK,"成功");
    }

    //3.3 用户修改-修改用户密码
    @ApiOperation(value = "修改用户密码",notes = "修改用户密码")
    @ApiImplicitParam(name = "Newpassword",value = "新密码",dataType = "String",required = true,paramType = "query")
    @ResponseBody
    @RequestMapping(value = "/updateUserPwd",method = RequestMethod.POST)
    public Result updateUserPwd(@RequestParam("Newpassword") String Newpassword,@RequestHeader("define-token") String language){
        int userint = getUserIDByToken(language);
        //将密码进行MD5加密
        Md5Hash pwdmd5 = new Md5Hash(Newpassword,"lq", 1);
        int i = userService.updatePassword(pwdmd5.toString(), userint);
        return new Result(true,StatusCode.OK,"成功");
    }

}
